﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Web.Support;
using Cn.Edwon.Spring;
using Cn.Cellula.Biz.Port;
using System.Reflection;
using Cn.Cellula.Model.System.Wf;
using Cn.Edwon.Web.Validation;
using Cn.Cellula.Client;
using Cn.Edwon.Web;

namespace Cn.Cellula.Website.Admin.Wf.Config {
    public partial class EditWf : PageSupport {
        String EOKey = null;
        IWfBiz biz = BizFactory.Get<IWfBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            EOKey = Request.QueryString["id"];
            if (!Page.IsPostBack) {
                InitForm();
                if (!String.IsNullOrEmpty(EOKey)) {
                    FillForm(EOKey);
                }
            }
        }
        void InitForm() {
        }
        void FillForm(String key) {
            TSysWfEO eo = biz.Load<TSysWfEO>(key);
            this.txt_Desc.Text = eo.WfDesc;
            this.txt_WfName.Text = eo.WfName;
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            Boolean bk = QuickValidator.Execute(Request, this.txt_WfName.Parent);
            if (!bk) {
                SiteInfo.Send(Page, TxtDef.ERROR, QuickValidator.Message);
                return;
            }
            TSysWfEO eo = null;
            if (String.IsNullOrEmpty(EOKey)) {
                eo = new TSysWfEO();
                eo.WfName = this.txt_WfName.Text;
                eo.WfDesc = this.txt_Desc.Text;
                eo.CreateDt = DateTime.Now;
                eo.LastDt = DateTime.Now;
                eo.OwnerKey = this.OwnerKey;
                eo.Operator = (this.Actor == null) ? "" : this.Actor.ID;
                Object newKey = biz.Save<TSysWfEO>(eo);
                BizFactory.Get<ILogBiz>().LogInsert("流程定义", eo.WfName, this.Actor, Request);
                Response.Redirect("List.aspx?wid=" + newKey);
            } else {
                eo = biz.Load<TSysWfEO>(EOKey);
                eo.WfDesc = this.txt_Desc.Text;
                eo.WfName = this.txt_WfName.Text;
                eo.LastDt = DateTime.Now;
                eo.Operator = (this.Actor == null) ? "" : this.Actor.ID;
                biz.Update<TSysWfEO>(eo);
                BizFactory.Get<ILogBiz>().LogUpdate("对象", eo.WfName, this.Actor, Request);
                Response.Redirect("List.aspx?wid=" + eo.WfKey);
            }
        }

    }
}